﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Skyworth.CRM.Common;
using Skyworth.CRM.Service.IBLL;

namespace Skyworth.CRM.Web.SystemManagement
{
    public partial class Roles : BasePage
    {
        private IRoleBLL roleBll=IocContainer.Instance.Resolve<IRoleBLL>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ControlAuthority(new List<WebControl> { btnAdd, btnUpdate, btnDelete,btnSearch }, 28);
                this.RoleTableBody.Text = GetRoleInfo(string.Empty, null);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                string roleName = txtRoleName.Text.Trim();
                if (!string.IsNullOrEmpty(roleName))
                {
                    Hashtable parameter = new Hashtable();
                    parameter.Add("roleName", "%" + roleName + "%");
                    this.RoleTableBody.Text = GetRoleInfo("and RoleName like @roleName", parameter);
                }
                else
                {
                    this.RoleTableBody.Text = GetRoleInfo("", null);
                }
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
                Alert("发生异常：\r\n" + exception.Message);
            }
            

        }

        private string GetRoleInfo(string strWhere,Hashtable parameter)
        {
            StringBuilder strRoleTable = new StringBuilder();
            DataTable dtRole = roleBll.Query(strWhere,parameter);
            if(dtRole!=null && dtRole.Rows.Count>0)
            {
                foreach (DataRow dataRow in dtRole.Rows)
                {
                    strRoleTable.AppendFormat("<tr rel='{0}'>", dataRow["RoleId"]);
                    strRoleTable.AppendFormat("<td><a herf='javascript:void(0)' class='openWindow' id='"+dataRow["RoleId"]+"'>角色权限</a></td><td>{0}</td><td>{1}</td></tr>", dataRow["RoleName"], dataRow["RoleDescription"]);
                }
            }
            return strRoleTable.ToString();
        }

        protected void btnhidDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int roleId = 0;
                int.TryParse(this.selectedRoleID.Value.Trim(), out roleId);
                roleBll.Delete(roleId);

                this.RoleTableBody.Text = GetRoleInfo(string.Empty, null);
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
                Alert("发生异常：\r\n" + exception.Message);
            }
            
            
        }
    }
}
